package nkr1pt.popkorn.extract;

import java.io.File;
import java.util.Set;
import nkr1pt.popkorn.download.DownloadCandidate;
import nkr1pt.popkorn.download.DownloadCandidateItem;
import org.apache.commons.lang.StringUtils;

/**
 * Leverages the Linux p7zip-rar command.
 *
 * @author Kristof Vanhaeren
 */
public class Linux7ZipCommand implements ExtractCommand {

    public Set<DownloadCandidateItem> testCRC(DownloadCandidate downloadCandidate) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean extract(DownloadCandidate downloadCandidate) throws Exception {
        DownloadCandidateItem item = downloadCandidate.getItems().iterator().next();
        String password = downloadCandidate.getArchivePassword();

        Process proc = null;
        if (StringUtils.isEmpty(password)) {
            proc = Runtime.getRuntime().exec("7z e " + item.getSimpleName(), null, new File(downloadCandidate.getExtractLocation()));
        } else {
            proc = Runtime.getRuntime().exec("7z e -p" + password + " " + item.getSimpleName(), null, new File(downloadCandidate.getExtractLocation()));
        }

        if (proc.waitFor() == 0) {
            return true;
        }

        return false;
    }

    public String getName() {
        return "p7zip-rar";
    }

}